$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'param/item/list',
        datatype: "json",
        colModel: [
            //{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
            { label: '参数名称', name: 'paramName', index: 'param_name', width: 80 },
            { label: '参数描述', name: 'paramContent', index: 'param_content', width: 80 },
            //{ label: '部门ID', name: 'deptId', index: 'dept_id', width: 80 },
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            //{ label: '创建人ID', name: 'createUser', index: 'create_user', width: 80 },
            { label: '创建人名字', name: 'createName', index: 'create_name', width: 80 },
            { label: '修改时间', name: 'updateTime', index: 'update_time', width: 80 },
            //{ label: '修改人ID', name: 'updateUser', index: 'update_user', width: 80 },
            { label: '修改人名字', name: 'updateName', index: 'update_name', width: 80 }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList : [10,30,50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page",
            rows:"limit",
            order: "order"
        },
        gridComplete:function(){
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
        }
    });
});

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        paramItem: {},
        q: {
            paramName : ""
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.paramItem = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if(id == null){
                return ;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            if($("#form1").validate().form()){
                $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                    var url = vm.paramItem.id == null ? "param/item/save" : "param/item/update";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.paramItem),
                        success: function(r){
                            if(r.code === 0){
                                layer.msg("操作成功", {icon: 1});
                                vm.reload();
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                            }else{
                                layer.alert(r.msg);
                                $('#btnSaveOrUpdate').button('reset');
                                $('#btnSaveOrUpdate').dequeue();
                            }
                        }
                    });
                });
            }
        },
        del: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
                if(!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "param/item/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function(){
            });
        },
        getInfo: function(id){
            $.get(baseURL + "param/item/info/"+id, function(r){
                vm.paramItem = r.paramItem;
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam','page');
            $("#jqGrid").jqGrid('setGridParam',{
                postData:{'paramName': vm.q.paramName},
                page:page
            }).trigger("reloadGrid");
        }
    }
});